# -*- encoding: utf-8 -*-
import os
from datetime import datetime


def delete_dir(path):
    if os.path.isdir(path):
        dir_name = os.listdir(path)
        for file in dir_name:
            delete_dir(os.path.join(path, file))
        os.rmdir(path)
    else:
        if os.path.exists(path):
            os.remove(path)


def exists_dir(dir_name):
    project_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    log_dir = os.path.join(project_dir, dir_name)
    if os.path.exists(log_dir):
        delete_dir(log_dir)
    os.mkdir(log_dir)
    return log_dir


def create_log(dir_name='log'):
    now = datetime.now()
    return os.path.join(exists_dir(dir_name), "{}_{}_{}.log".format(now.year, now.month, now.day))
